import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import MusicList from '../views/MusicList.vue'
import SearchView from '../views/SearchView.vue'
import Login from '../views/Login.vue'
import User from '../views/User.vue'
const routes = [
  {
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: '/home',
    redirect: '/'
  },
  {
    path: '/musicList',
    name: 'MusicList',
    component: MusicList
  },
  {
    path: '/search',
    name: 'search',
    component: SearchView
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/user',
    name: 'user',
    component: User
  }
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to,from,next)=>{
  if (to.path === '/user') {
    const token = window.sessionStorage.getItem('token')
    if (!token) {
      return next('/login')
    }else {
      next()
    }
  }
  next()
})
export default router
